package com.example.weatherdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import com.example.weatherdemo.adapter.MyFragmentStateVPAdapter;
import com.example.weatherdemo.fragment.VPFragment;
import com.example.weatherdemo.fragment.VPHomeFragment;
import com.google.android.material.badge.BadgeDrawable;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager mViewPager;
    private BottomNavigationView mbottomNavigationView;

    //1、将刚刚写好的Adapter声明出来
    private MyFragmentStateVPAdapter mStateVPAdapter;
    //数据源
    private List<Fragment> mFragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mViewPager = (ViewPager) findViewById(R.id.vp);
        mbottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_menu);

        initData();

        //2、创建Adapter
        mStateVPAdapter = new MyFragmentStateVPAdapter(getSupportFragmentManager(), mFragmentList);

        mViewPager.setAdapter(mStateVPAdapter);

        //3、设置ViewPager的滑动监听
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Toast.makeText(MainActivity.this,
                        "页面" + (position + 1),
                        Toast.LENGTH_SHORT).show();

                onPagerSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //4、为view设置点击事件的监听
        mbottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.menu_home:
                        mViewPager.setCurrentItem(0);
                        break;
                    case R.id.menu_find:
                        mViewPager.setCurrentItem(1);
                        break;
                    case R.id.menu_mine:
                        mViewPager.setCurrentItem(2);
                        break;
                    default:
                        break;
                }
                return true;
            }
        });

        //5、小红点
        BadgeDrawable badge = mbottomNavigationView.getOrCreateBadge(R.id.menu_mine);
        badge.setMaxCharacterCount(3);
        badge.setNumber(100);

    }


    /**
     * 对选择的position进行情况处理
     * 为底部按钮进行联动
     *
     * @param position
     */
    private void onPagerSelected(int position) {
        switch (position) {
            case 0:
                mbottomNavigationView.setSelectedItemId(R.id.menu_home);
                break;
            case 1:
                mbottomNavigationView.setSelectedItemId(R.id.menu_find);
                break;
            case 2:
                //消除menu_mine上的小红点
                mbottomNavigationView.removeBadge(R.id.menu_mine);
                mbottomNavigationView.setSelectedItemId(R.id.menu_mine);
                break;
            default:
                break;
        }
    }

    private void initData() {
        mFragmentList = new ArrayList<>();

        VPHomeFragment fragmentHome = VPHomeFragment.newInstance("首页", "");
//        VPFragment fragmentHome = VPFragment.newInstance("首页", "");
        VPFragment fragmentFind = VPFragment.newInstance("发现", "");
        VPFragment fragmentMine = VPFragment.newInstance("我的", "");

        mFragmentList.add(fragmentHome);
        mFragmentList.add(fragmentFind);
        mFragmentList.add(fragmentMine);


    }
}